﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Reflection;
using System.Text;
using ConfigGen.Utilities.Extensions.System.Reflection;
using ConfigGen.Utilities.Xml;
using NUnit.Framework;

namespace ConfigGen.Utilities.Tests.Xml
{
    /// <summary>
    /// Tests to assert correct behaviour of XmlDeclarationParser. DeveloperNote: a lot of the actual encoding detection being tested is more of a test of XmlTextReader 
    /// than the code XmlDeclarationParser. For the time being it is here to assert *to me* that I understand how this behaves!
    /// </summary>
    [TestFixture]
    public class XmlDeclarationParserTests
    {
        [Test]
        public void Getters001_ThrowExceptionIfAccessedBeforeParse()
        {
            var xmlDeclarationParser = new XmlDeclarationParser();

            Assert.Throws<InvalidOperationException>(() => { var i = xmlDeclarationParser.ActualEncoding; }, "ActualEncoding should have thrown an InvalidOperationException");
            Assert.Throws<InvalidOperationException>(() => { var i = xmlDeclarationParser.StatedEncoding; }, "StatedEncoding should have thrown an InvalidOperationException");
            Assert.Throws<InvalidOperationException>(() => { var i = xmlDeclarationParser.XmlDeclarationPresent; }, "XmlDeclarationPresent should have thrown an InvalidOperationException");

        }

        [Test]
        public void Parse001_UTF8()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-WithDeclaration-UTF8.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsTrue(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsInstanceOf(typeof(UTF8Encoding), xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.AreEqual(65001, xmlDeclarationParser.StatedEncoding.CodePage, "Incorrect StatedEncoding.CodePage"); // utf-8
                Assert.IsInstanceOf(typeof(UTF8Encoding), xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");
                Assert.AreEqual(65001, xmlDeclarationParser.StatedEncoding.CodePage, "Incorrect StatedEncoding.CodePage"); // utf-8
            }
        }

        [Test]
        public void Parse002_UTF16()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-WithDeclaration-UTF16.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsTrue(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsInstanceOf(typeof(UnicodeEncoding), xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.AreEqual(1200, xmlDeclarationParser.StatedEncoding.CodePage, "Incorrect StatedEncoding.CodePage"); // unicode little-endian
                Assert.IsInstanceOf(typeof(UnicodeEncoding), xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");
                Assert.AreEqual(1200, xmlDeclarationParser.ActualEncoding.CodePage, "Incorrect ActualEncoding.CodePage"); // unicode little-endian
            }
        }

        [Test]
        public void Parse003_ANSI()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-WithDeclaration-ANSI.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsTrue(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsNull(xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.IsInstanceOf(typeof(UTF8Encoding), xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");
            }
        }

        [Test]
        public void Parse004_Windows1252()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-WithDeclaration-ANSI-Windows1252.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsTrue(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsNotNull(xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.AreEqual(1252, xmlDeclarationParser.StatedEncoding.CodePage, "Incorrect StatedEncoding.CodePage"); // windows-1252
                Assert.IsNotNull(xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");
                Assert.AreEqual(1252, xmlDeclarationParser.ActualEncoding.CodePage, "Incorrect ActualEncoding.CodePage");  // windows-1252
            }
        }

        [Test]
        public void Parse005_UTF16_NoDeclaration()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-NoDeclaration-UTF16.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsFalse(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsNull(xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.IsNotNull(xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");
                Assert.AreEqual(1200, xmlDeclarationParser.ActualEncoding.CodePage, "Incorrect ActualEncoding.CodePage");  // unicode little endian
            }
        }

        /// <summary>
        /// Regression test: http://configgen.codeplex.com/workitem/1
        /// </summary>
        [Test]
        public void Parse006_UTF8_WithUTF16Declaration()
        {
            using (var readerStream = Assembly.GetExecutingAssembly().GetEmbeddedResourceFileStream("TestResources.Simple-UTF8-File-With-UTF16-Declaration.xml"))
            {
                var xmlDeclarationParser = new XmlDeclarationParser();
                xmlDeclarationParser.Parse(readerStream);
                Assert.IsTrue(xmlDeclarationParser.XmlDeclarationPresent, "Incorrect DeclarationPresent");
                Assert.IsNotNull(xmlDeclarationParser.StatedEncoding, "Incorrect StatedEncoding");
                Assert.IsNotNull(xmlDeclarationParser.ActualEncoding, "Incorrect ActualEncoding");

                Assert.AreEqual(1200, xmlDeclarationParser.StatedEncoding.CodePage, "Incorrect StatedEncoding.CodePage");  // unicode little endian
                Assert.AreEqual(65001, xmlDeclarationParser.ActualEncoding.CodePage, "Incorrect ActualEncoding.CodePage");  // unicode little endian
            }
        }
    }
}
